import { useDictsContext } from '../context/dicts-context'
import { DictsActionDialog } from './dicts-action-dialog'
import { DictsDeleteDialog } from './dicts-delete-dialog'

export function DictsDialogs() {
  const { open, setOpen, currentRow, setCurrentRow } = useDictsContext()
  return (
    <>
      <DictsActionDialog
        key='dict-add'
        open={open === 'add'}
        onOpenChange={() => setOpen('add')}
      />

      {currentRow && (
        <>
          <DictsActionDialog
            key={`dict-edit-${currentRow.id}`}
            open={open === 'edit'}
            onOpenChange={() => {
              setOpen('edit')
              setTimeout(() => {
                setCurrentRow(null)
              }, 500)
            }}
            currentRow={currentRow}
          />

          <DictsDeleteDialog
            key={`dict-delete-${currentRow.id}`}
            open={open === 'delete'}
            onOpenChange={() => {
              setOpen('delete')
              setTimeout(() => {
                setCurrentRow(null)
              }, 500)
            }}
            currentRow={currentRow}
          />
        </>
      )}
    </>
  )
}
